package com.shaoshaohuo.app.ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.adapter.MyBaseAdapter;
import com.shaoshaohuo.app.entity.RemarkEntitiy;
import com.shaoshaohuo.app.net.ShopHttpConfig;
import com.shaoshaohuo.app.utils.CircleImageView;
import com.shaoshaohuo.app.utils.FiveCornerStar;
import com.shaoshaohuo.app.utils.Okhttputils;
import com.shaoshaohuo.app.utils.ToastUtil;
import com.squareup.picasso.Picasso;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

/**
 *
 * 评价界面
 * Created by Administrator on 2017/6/26.
 */

public class DetailsFragmentBottomTow extends Fragment implements View.OnClickListener {


    private View inflate;

    private String catid;
    private Mydapter mydapter;
    private LinearLayout lin_tv;

    public static DetailsFragmentBottomTow newInstance(String catid) {

        Bundle args = new Bundle();
        args.putString("catid", catid);
        DetailsFragmentBottomTow fragment = new DetailsFragmentBottomTow();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

//        TextView textView = new TextView(getContext());
//        textView.setText("评价");
//        textView.setGravity(Gravity.CENTER);
        View inflate = inflater.inflate(R.layout.frag_detailsbottom_tow, null);
        Bundle arguments = getArguments();
        catid = arguments.getString("catid");

        return inflate;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init(view);
    }

    private void init(View view) {
        view.findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().finish();
            }
        });
        ListView frag_detailsbottomtow_listview = (ListView) view.findViewById(R.id.frag_detailsbottomtow_listview);
        inflate = LayoutInflater.from(view.getContext()).inflate(R.layout.frag_details_btom_listhead, null);
        frag_detailsbottomtow_listview.addHeaderView(inflate);
        mydapter = new Mydapter(getContext(), new ArrayList<RemarkEntitiy.DataBean>(), false);
        frag_detailsbottomtow_listview.setAdapter(mydapter);

        lin_tv = (LinearLayout) ((LinearLayout) inflate).getChildAt(0);
        for (int i = 0; i < lin_tv.getChildCount(); i++) {
            lin_tv.getChildAt(i).setOnClickListener(this);
        }
        lin_tv.getChildAt(0).setSelected(true);
        network();


    }

    private void network() {
        int type = -1;
        for (int i = 0; i < lin_tv.getChildCount(); i++) {
            TextView childAt = (TextView) lin_tv.getChildAt(i);
            if (childAt.isSelected()) {
                type = i + 1;
                break;
            }

        }
        if (type == -1) {
            type = 4;
        }
        ShopHttpConfig.remarkqurey(getContext(), type + "", catid, new Okhttputils.OkutListener() {
            @Override
            public void error(Call call, IOException e) {
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ToastUtil.toast("您的网络飞啦~~~");
                        mydapter.setData(new ArrayList<RemarkEntitiy.DataBean>());
                    }
                });
            }

            @Override
            public void error(Call call, int code) {
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mydapter.setData(new ArrayList<RemarkEntitiy.DataBean>());
                    }
                });
            }

            @Override
            public void suseff(Call call, Response response, Object o) {
                final RemarkEntitiy remarkEntitiy = (RemarkEntitiy) o;
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mydapter.setData(remarkEntitiy.getData());
                    }
                });

            }
        }, RemarkEntitiy.class);
    }

    @Override
    public void onClick(View v) {
        for (int i = 0; i < ((ViewGroup) lin_tv).getChildCount(); i++) {
            View childAt = ((ViewGroup) lin_tv).getChildAt(i);
            (childAt).setSelected(false);
//            if (i == 0) {
//                LinearLayout linearLayout = (LinearLayout) childAt;
//                for (int a = 0; a < linearLayout.getChildCount(); a++) {
//                    ((TextView) linearLayout.getChildAt(a)).setSelected(false);
//                }
//
//            } else {
//                CheckBox checkBox = (CheckBox) childAt;
//                checkBox.setSelected(true);
//            }
        }
        (lin_tv.getChildAt(lin_tv.indexOfChild(v))).setSelected(true);
//        if (lincheck.indexOfChild(v) != -1) {
//            ((CheckBox) lincheck.getChildAt(lincheck.indexOfChild(v))).setChecked(true);
//        } else {
//            childAt.setChecked(true);
//        }
        network();
    }

    class Mydapter extends MyBaseAdapter<RemarkEntitiy.DataBean> implements View.OnClickListener {


        private final LayoutInflater from;

        public Mydapter(Context ctx, List<RemarkEntitiy.DataBean> list, boolean flag) {
            super(ctx, list, flag);
            from = LayoutInflater.from(ctx);
        }

        @Override
        public View getView(int pos, View view, ViewGroup group) {
            ViewHolder viewHolder;
            if (view == null) {
                viewHolder = new ViewHolder();
                view = from.inflate(R.layout.listitem_details_rem, null);
                viewHolder.fiveCornerStar = (FiveCornerStar) view.findViewById(R.id.listitem_details_FiveCornerStar);
                viewHolder.listitem_details_photo = (CircleImageView) view.findViewById(R.id.listitem_details_photo);
                viewHolder.listitem_details_tv_phonenum = (TextView) view.findViewById(R.id.listitem_details_tv_phonenum);
                viewHolder.listitem_details_tvtime = (TextView) view.findViewById(R.id.listitem_details_tvtime);
                viewHolder.listitem_details_tv_value = (TextView) view.findViewById(R.id.listitem_details_tv_value);
                viewHolder.listitem_details_photolist = (LinearLayout) view.findViewById(R.id.listitem_details_photolist);
                view.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) view.getTag();
            }
            viewHolder.fiveCornerStar.setIsdown(false);
            viewHolder.fiveCornerStar.setNum(5, BitmapFactory.decodeResource(getResources(),R.mipmap.star).getWidth(),BitmapFactory.decodeResource(getResources(),R.mipmap.star).getHeight());
            RemarkEntitiy.DataBean dataBean = list.get(pos);

            String remarkLevel = dataBean.getRemarkLevel();
            viewHolder.fiveCornerStar.setdownnum(Float.valueOf(remarkLevel));
            String remarkTime = dataBean.getRemarkTime();
            viewHolder.listitem_details_tvtime.setText(remarkTime);
            String remarkValue = dataBean.getRemarkValue();
            viewHolder.listitem_details_tv_value.setText(remarkValue);
            String userImg = dataBean.getUserImg();
            if (userImg.length() > 0) {
                Picasso.with(getContext()).load(userImg).into(viewHolder.listitem_details_photo);
            }
            String userPhone = dataBean.getUserPhone();
            String substring = userPhone.substring(3, 7);
            String replace = userPhone.replace(substring, "****");
            viewHolder.listitem_details_tv_phonenum.setText(replace);
            List<String> imgMap = dataBean.getImgMap();
            viewHolder.listitem_details_photolist.removeAllViews();
            for (int i = 0; i < imgMap.size(); i++) {
                ImageView imageView = new ImageView(getContext());
                imageView.setTag(pos);
                imageView.setOnClickListener(this);
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                if (imgMap.get(i).length() > 0) {
                    Picasso.with(getContext()).load(imgMap.get(i)).into(imageView);
                }
                viewHolder.listitem_details_photolist.addView(imageView);
                LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) imageView.getLayoutParams();
                layoutParams.width = (int) getResources().getDimension(R.dimen.image_size);
                layoutParams.height = (int) getResources().getDimension(R.dimen.image_size);
                layoutParams.setMargins(0, 0, (int) getResources().getDimension(R.dimen.imamarn), 0);

            }

            return super.getView(pos, view, group);
        }

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(context, SpaceImageDetailActivity.class);
            int tag = (int) v.getTag();
            List<String> imgMap = list.get(tag).getImgMap();
            LinearLayout parent = (LinearLayout) v.getParent();
            int i = parent.indexOfChild(v);
            int[] location = new int[2];
            v.getLocationOnScreen(location);
            intent.putExtra("locationX", location[0]);
            intent.putExtra("locationY", location[1]);
            intent.putExtra("images",imgMap.get(i));
            intent.putExtra("width", v.getWidth());
            intent.putExtra("height", v.getHeight());
            startActivity(intent);
            getActivity().overridePendingTransition(0, 0);
        }

        class ViewHolder {
            TextView listitem_details_tv_phonenum, listitem_details_tvtime, listitem_details_tv_value;
            CircleImageView listitem_details_photo;
            FiveCornerStar fiveCornerStar;
            LinearLayout listitem_details_photolist;
        }
    }
}
